<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admins
 *
 * @author Maulnick
 */
class Message_Model extends CI_Model {
    //***************/
    //  RETRIEVAL
    //***************/
    function get_message_id_by_date($date){
        $this->db->where('date',$date);
        $result = $this->db->get('message');
        if($result->num_rows==1){
            return $result->row(0)->message_id;
        }else{
            return false;
        }
    }
    function get_message($message_id){
        $this->db->where('message_id',$message_id);
        $result = $this->db->get('message');
        if($result->num_rows==1){
            return $result->row(0);
        }
    }
    function get_recipient($message_id, $sender_role_id, $sender_user_id){
        $this->db->select('role_user_id, user_id');
        $this->db->where('message_id',$message_id);
        $this->db->where('role_user_id !=',$sender_role_id);
        $this->db->where('user_id !=',$sender_user_id);
        $result = $this->db->get('user_message_map');
        if($result->num_rows==1){
            return $result->row(0);
        }
    }
    function get_message_document($message_id){
        $this->db->where('message_id',$message_id);
        $result = $this->db->get('message_document');
        if($result->num_rows>0){
            return $result->result();
        }
    }
    function get_user_message_map($user_message_map_id){
        $this->db->where('user_message_map_id',$user_message_map_id);
        $result = $this->db->get('user_message_map');
        if($result->num_rows==1){
            return $result->row(0);
        }
    }
    function get_user_message_map_by_message_user($message_id,$role_user_id,$user_id){
        $this->db->where('message_id',$message_id);
        $this->db->where('role_user_id',$role_user_id);
        $this->db->where('user_id',$user_id);
        $result = $this->db->get('user_message_map');
        if($result->num_rows==1){
            return $result->row(0);
        }
    }
    function get_message_document_by_role_id($message_id,$role_doc_id,$doc_id){
        $this->db->where('message_id',$message_id);
        $this->db->where('role_document_id',$role_doc_id);
        $this->db->where('document_id',$doc_id);
        $result = $this->db->get('message_document');
        if($result->num_rows==1){
            return $result->row(0);
        }
    }
    function get_opponent_user_by_document($role_user_id, $user_id, $role_doc_id,$doc_id){
        $this->db->where('role_document_id',$role_doc_id);
        $this->db->where('document_id',$doc_id);
        $result = $this->db->get('message_document');
        if($result->num_rows==1){
            $message_document = $result->row(0);
            $this->db->where('message_id',$message_document->message_id);
            $this->db->where('role_user_id !=',$role_user_id);
            $this->db->where('user_id !=',$user_id);
            $result = $this->db->get('user_message_map');
            if ($result->num_rows==1){
                return $result->row(0);
            }
        }
    }
    //***************/
    //  INSERT
    //***************/
    public function insert_message($message){
        return $this->db->insert('message',$message);
    }
    public function insert_user_message_map($message_id, $from_role_id, $from_user_id, $to_role_id, $to_user_id){
        // called when send a message,
        // create outbox and inbox
        $data = array(
           array(
              'message_id' => $message_id,
              'role_user_id' => $from_role_id,
              'user_id' => $from_user_id,
              'message_placeholder_id' => MESSAGE_OUTBOX,
              'is_read' => false
           ),
           array(
              'message_id' => $message_id,
              'role_user_id' => $to_role_id,
              'user_id' => $to_user_id,
              'message_placeholder_id' => MESSAGE_INBOX,
              'is_read' => false
           )
        ); 
        return $this->db->insert_batch('user_message_map', $data);
    }
    
    public function insert_attachment($message_id, $role_document_id, $doc_id, $file_url){
        // called when send a message,
        // create outbox and inbox
        $data['message_id'] = $message_id;
        $data['role_document_id'] = $role_document_id;
        $data['document_id'] = $doc_id;
        $data['file_url'] = $file_url;
        return $this->db->insert('message_document', $data);
    }
    //***************/
    //  UPDATE
    //***************/
    public function update_is_read($user_message_map_id){
        $data = array(
               'is_read' => true
            );
        $this->db->where('user_message_map_id', $user_message_map_id);
        $this->db->update('user_message_map', $data); 
    }
    //***************/
    //  TESTING
    //***************/
    
}

